package com.itheima.reggie.contronller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.LoginCode;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealContronller {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CacheManager cacheManager;
    /**
     * 保存套餐以及菜品信息
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
//打印套餐信息
        log.info(setmealDto.toString());

        setmealService.saveSetmealWithDish(setmealDto);


        return R.success("套餐添加成功");

    }

    /**
     * 分页查询套餐表以及套餐分类
     */
    @ApiOperation(value = "套餐分页查询接口")
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {

        Page<SetmealDto> dto = setmealService.pageWithCategory(page, pageSize, name);
        return R.success(dto);
    }

    @DeleteMapping
    @ApiOperation(value = "套餐删除接口")
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("ids:{}", ids);
        setmealService.deleteWithDish(ids);

        return R.success("删除成功");
    }

    /**
     * 移动端展示套餐数据
     * @param categoryId
     * @param status
     * @return
     */
    @ApiOperation(value = "套餐条件查询接口")
    @Cacheable(value = LoginCode.SETMEALLIST_REDIS_KEY,key = "#categoryId + '_' + #status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status)
                .orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> setmeals = setmealService.list(lqw1);

        return R.success(setmeals);
    }

    /**
     * 查询套餐下的菜品信息
     *
     * @return
     */
    @ApiOperation(value = "查询套餐下的菜品信息")
    @GetMapping("/dish/{id}")
    public R<List> dish(@PathVariable Long id) {


        List<DishDto> dishList = setmealService.getDish(id);

        return R.success(dishList);
    }

    /**
     * 批量修改套餐状态
     *
     * @param status
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量修改套餐状态")
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        log.info("status:{},ids:{}", status, ids);

        setmealService.updateStatus(status, ids);
        return R.success("套餐状态修改成功");
    }

    /**
     * 根据id进行查询套餐,及套餐下的菜品信息
     */
    @ApiOperation(value = "根据id进行查询套餐,及套餐下的菜品信息")
    @GetMapping("/{id}")
    public R<SetmealDto> getWithDish(@PathVariable Long id) {

        SetmealDto setmealDto = setmealService.getWithDish(id);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改套餐信息")
    public R<String> updateWithDish(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto:{}", setmealDto);

        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }
}
